﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Pantry.Commons;
using Pantry.Entity;

namespace Pantry.DataAccess
{
    public class RoleDao
    {
        private readonly SqlConnection _sqlConnection;
        /// <summary>
        /// Initializes a new instance of the <see cref="RoleDao"/> class.
        /// </summary>
        public RoleDao()
        {
            _sqlConnection = SqlConnectionTool.SqlGetConnection();
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="RoleDao"/> class.
        /// </summary>
        /// <param name="connectionString">The connection string.</param>
        public RoleDao(string connectionString)
        {
            _sqlConnection = new SqlConnection(connectionString);
        }
        /// <summary>
        /// Lists the roles.
        /// </summary>
        /// <returns></returns>
        public DataTable ListRoles()
        {
            try
            {
                var roleTable = SqlHelper.ExecuteDataSet(_sqlConnection, CommandType.Text, DBSchemas.ListRole).Tables[0];
                return roleTable;
            }
            catch (SqlException exception)
            {
                ExceptionHandler.WriteErrorLog(exception);
                return null;
            }
        }
        /// <summary>
        /// Roles the name by id.
        /// </summary>
        /// <param name="roleId">The role id.</param>
        /// <returns></returns>
        public string RoleNameById(int roleId)
        {
            try
            {
                var parameter = new SqlParameter("@RoleId", SqlDbType.Int) { Value = roleId };
                var roleName = SqlHelper.ExecuteScalar(_sqlConnection, CommandType.Text, DBSchemas.RoleNameById,
                                                         parameter).ToString();
                return roleName;
            }
            catch (SqlException exception)
            {
                ExceptionHandler.WriteErrorLog(exception);
                return null;
            }
        }
        /// <summary>
        /// Adds the role.
        /// </summary>
        /// <param name="role">The role.</param>
        /// <returns></returns>
        public int AddRole(Role role)
        {
            try
            {
                var parameter = new SqlParameter("@RoleName", SqlDbType.NVarChar) {Value = role.RoleName};
                return SqlHelper.ExecuteNonQuery(_sqlConnection, CommandType.Text, DBSchemas.AddRole, parameter);
            }
            catch (SqlException sqlException)
            {
                ExceptionHandler.WriteErrorLog(sqlException);
                return 0;
            }
        }
        /// <summary>
        /// Lists the role by id.
        /// </summary>
        /// <param name="organizationId">The organization id.</param>
        /// <returns></returns>
        public DataTable ListRoleById(int organizationId)
        {
            try
            {
                var parameter = new SqlParameter("@OrganizationId", SqlDbType.Int) {Value = organizationId};
                return
                    SqlHelper.ExecuteDataSet(_sqlConnection, CommandType.Text, DBSchemas.ListRoleById, parameter).Tables
                        [0];
            }
            catch (SqlException exception)
            {
                ExceptionHandler.WriteErrorLog(exception);
                return null;
            }   
        }
    }
}
